const express =require('express')

const app=express()
const session =require('express-session')
app.use('/01',express.static('./HTMLTEST'))
app.use(express.urlencoded({ extended: false }))
app.use(session({
    secret:'itheima',
    resave:false,
    saveUninitialized:true
}))
// 登录api接口
app.post('/api/login',(req,res)=>{
    console.log('收到请求');
    if(req.body.username=='admin'&&req.body.password=='123456'){
       req.session.user=req.body
    //    设置用户登录状态
       req.session.islogin=true
       res.send({status:0,message:'恭喜你登录成功'})
       return
    }
    res.send({status:1,message:'登录失败！'})
})
// 获取数据api接口
app.get('/api/username',(req,res)=>{
    if(req.session.islogin){
        res.send({
            status: 0,
            msg: 'success',
            username: req.session.user.username,
          })
          return
    }
    res.send({status:1,message:'fail'})
})

// 退出登录接口
app.post('/api/logout',(req,res)=>{
    res.session.destroy()
    res.send({statsu:0,message:'退出登录成功'})
})
app.listen(80,()=>{
    console.log('创建服务器成功');
})